package com.nsgf.library.utils;

import static android.graphics.Color.alpha;
import static android.graphics.Color.argb;
import static android.graphics.Color.blue;
import static android.graphics.Color.green;
import static android.graphics.Color.red;
import static android.graphics.Paint.Style.STROKE;
import static java.lang.Math.PI;
import static java.lang.StrictMath.cos;
import static java.lang.StrictMath.sin;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.ClipboardManager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

/**
 * Created by King on 2017/12/18 0018.
 * dp px
 * 常用工具类
 */
public class UnitTo {

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    //textview drawable
    public static void setBorder(Context context, int strokeColor, String borderColor, int TextColor, TextView textView) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(UnitTo.px(context, 2));
        drawable.setColor(Color.parseColor(borderColor));
        drawable.setStroke(UnitTo.px(context, 0.5f), context.getResources().getColor(strokeColor));
        textView.setTextColor(context.getResources().getColor(TextColor));
        textView.setBackground(drawable);
    }

    //textview drawable
    public static void setBorder(Context context, int strokeColor, int borderColor, int border, int TextColor, TextView textView) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(UnitTo.px(context, border));
        drawable.setColor(context.getResources().getColor(borderColor));
        drawable.setStroke(UnitTo.px(context, 0.5f), context.getResources().getColor(strokeColor));
        textView.setTextColor(context.getResources().getColor(TextColor));
        textView.setBackground(drawable);
    }

    //textview drawable
    public static void setBorderMarker(Context context, int strokeColor, int borderColor, int border, int TextColor, TextView textView) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadii(new float[]{border, border, 0, 0, border, border, 0, 0});
        drawable.setColor(context.getResources().getColor(borderColor));
        drawable.setStroke(UnitTo.px(context, 0.5f), context.getResources().getColor(strokeColor));
        textView.setTextColor(context.getResources().getColor(TextColor));
        textView.setBackground(drawable);
    }

    /**
     * @param context
     * @param strokeColor
     * @param borderColor
     * @param TextColor
     * @param textView
     * @param radius
     */
    public static void setBorder(Context context, int strokeColor, int borderColor, int TextColor, TextView textView, int radius) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(UnitTo.px(context, radius));
        drawable.setColor(context.getResources().getColor(borderColor));
        drawable.setStroke(UnitTo.px(context, 0.5f), context.getResources().getColor(strokeColor));
        textView.setTextColor(context.getResources().getColor(TextColor));
        textView.setBackground(drawable);
    }

    public static void setBorder(Context context, int startColor, int endColor, TextView textView) {
        int[] colors = {startColor, endColor};
        GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
        drawable.setCornerRadius(UnitTo.px(context, 2));
        textView.setBackground(drawable);
    }

    public static void setBorder(Context context, int startColor, int endColor, TextView textView,
                                 float mTopLeftRadius, float mTopRightRadius,
                                 float mBottomLeftRadius, float mBottomRightRadius) {
        int[] colors = {startColor, endColor};
        float[] radius = {mTopLeftRadius, mTopLeftRadius, mTopRightRadius, mTopRightRadius, mBottomRightRadius,
            mBottomRightRadius, mBottomLeftRadius, mBottomLeftRadius};
        GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
        drawable.setCornerRadii(radius);
        textView.setBackground(drawable);
    }

    //textview drawable
    public static void setBorder(Context context, int strokeColor, String borderColor, TextView textView, int radius) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(UnitTo.px(context, radius));
        drawable.setColor(Color.parseColor(borderColor));
        drawable.setStroke(UnitTo.px(context, 0.5f), context.getResources().getColor(strokeColor));
        textView.setBackground(drawable);
    }

    public static void setBorder(Context context, int strokeColor, int textColor, TextView textView, int radius) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(UnitTo.px(context, radius));
        drawable.setColor(context.getResources().getColor(strokeColor));
        drawable.setStroke(UnitTo.px(context, 0.5f), context.getResources().getColor(strokeColor));
        textView.setTextColor(context.getResources().getColor(textColor));
        textView.setBackground(drawable);
    }


    //view drawable
    public static void setBorder(Context context, int strokeColor, String borderColor, View view) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(UnitTo.px(context, 2));
        drawable.setColor(Color.parseColor(borderColor));
        drawable.setStroke(UnitTo.px(context, 1f), context.getResources().getColor(strokeColor));
        view.setBackground(drawable);
    }

    //view drawable
    public static void setBorder(Context context, int strokeColor, String borderColor, View view, int radius) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(UnitTo.px(context, radius));
        drawable.setColor(Color.parseColor(borderColor));
        drawable.setStroke(UnitTo.px(context, 0.5f), context.getResources().getColor(strokeColor));
        view.setBackground(drawable);
    }

    public static boolean isMainProcess(Application app) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) app.getSystemService
            (Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
            .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return app.getApplicationInfo().packageName.equals(appProcess.processName);
            }
        }
        return false;
    }

    public static int sp(float sp, DisplayMetrics metrics) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, metrics);
    }

    public static int dp(float dp, DisplayMetrics metrics) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
    }

    public static int gradient(int startColor, int endColor, int factor, int steps) {
        final int alpha = between(alpha(startColor), alpha(endColor), factor, steps);
        final int red = between(red(startColor), red(endColor), factor, steps);
        final int green = between(green(startColor), green(endColor), factor, steps);
        final int blue = between(blue(startColor), blue(endColor), factor, steps);
        return argb(alpha, red, green, blue);
    }

    public static void mutatePaint(Paint paint, int color, float strokeWidth, Paint.Style style) {
        paint.setColor(color);
        paint.setStrokeWidth(strokeWidth);
        paint.setStyle(style);

    }

    public static float[] createPoint(float radius, double alpha, float x0, float y0) {
        final float[] point = new float[2];
        point[0] = (float) (radius * cos(alpha) + x0);
        point[1] = (float) (radius * sin(alpha) + y0);
        return point;
    }

    public static float[] createPoints(int count, float radius, float x0, float y0) {
        final int length = count + count;
        final float[] points = new float[length];
        final double angle = 2 * PI / count;
        int j = 0;
        for (int i = 0; i < length; i += 2) {
            final double alpha = angle * j++ - PI / 2;
            final float[] point = createPoint(radius, alpha, x0, y0);
            points[i] = point[0];
            points[i + 1] = point[1];
        }
        return points;
    }

    public static int between(int startColor, int endColor, int factor, int steps) {
        final float ratio = (float) factor / steps;
        return (int) (endColor * ratio + startColor * (1 - ratio));
    }

    public static Paint createPaint(int color) {
        final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(STROKE);
        paint.setColor(color);
        return paint;
    }


    /**
     * 复制文本信息
     *
     * @param context
     * @param content
     */
    public static void copyStr(Context context, String content) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(content);
        ToastUtils.showShortToast("已复制内容到剪切板");
    }

    /**
     * 复制文本信息
     *
     * @param context
     * @param view
     */
    public static void copyStr(Context context, TextView view) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(view.getText());
        ToastUtils.showShortToast("已复制内容到剪切板");
    }

    public static Uri zoomPic(Activity context, Uri uri) {
        final String imageFileLocation = "file:///" + Environment.getExternalStorageDirectory().getPath() + "/temp.jpg";
        Uri imageUri = Uri.parse(imageFileLocation);
        Intent cut = new Intent("com.android.camera.action.CROP");
        cut.setDataAndType(uri, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        cut.putExtra("crop", "true");
        //该参数可以不设定用来规定裁剪区的宽高比
        cut.putExtra("aspectX", 1);
        cut.putExtra("aspectY", 1);
        //该参数设定为你的imageView的大小
        cut.putExtra("outputX", 800);
        cut.putExtra("outputY", 800);
        cut.putExtra("scale", true);
        //填充裁剪后内黑边
        cut.putExtra("scaleUpIfNeeded", true);
        //是否返回bitmap对象
        cut.putExtra("return-data", false);
        cut.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        cut.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//输出图片的格式
        cut.putExtra("noFaceDetection", true); // 头像识别
        context.startActivityForResult(cut, 3);
        return imageUri;
    }

}
